package com.hdax.gulimall.coupon.service.impl;

import com.hdax.common.to.MemberPrice;
import com.hdax.common.to.SkuReductionTo;
import com.hdax.gulimall.coupon.entity.MemberPriceEntity;
import com.hdax.gulimall.coupon.entity.SkuLadderEntity;
import com.hdax.gulimall.coupon.service.MemberPriceService;
import com.hdax.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.common.utils.PageUtils;
import com.hdax.common.utils.Query;

import com.hdax.gulimall.coupon.dao.SkuFullReductionDao;
import com.hdax.gulimall.coupon.entity.SkuFullReductionEntity;
import com.hdax.gulimall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {


    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {

        //1、保存满减打折，会员价
        SkuLadderEntity skuLadderEntity=new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
        skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
        skuLadderEntity.setDiscount(BigDecimal.valueOf(skuReductionTo.getDiscount()));
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
//        skuLadderEntity.setPrice();
        skuLadderService.save(skuLadderEntity);
        //1、保存满减
        SkuFullReductionEntity skuFullReduction=new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo,skuFullReduction);
        if(skuFullReduction.getFullPrice().compareTo(new BigDecimal("0"))==1){
            this.save(skuFullReduction);
        }

        //3、sms_member_price
        List<MemberPrice> memberPriceEntityList=new ArrayList<>();
        List<MemberPriceEntity> collect = memberPriceEntityList.stream().map(item -> {
            MemberPriceEntity memberPrice = new MemberPriceEntity();
            memberPrice.setSkuId(skuReductionTo.getSkuId());
            memberPrice.setMemberLevelId(item.getId());
            memberPrice.setMemberLevelName(item.getName());
            memberPrice.setMemberPrice(item.getPrice());
            memberPrice.setAddOther(1);
            return memberPrice;

        }).filter(eneity->{
            return eneity.getMemberPrice().compareTo(new BigDecimal("0"))==1;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(collect);
    }

}